#!/bin/bash

######## JAR 包定义 #######
jar_arr=(
app-chart,intradeframework-app-chart.jar,8890 
app-common,intradeframework-app-common.jar,8891 
app-finance,intradeframework-app-finance.jar,8898 
app-fund,intradeframework-app-fund.jar,8892 
app-mt,intradeframework-app-mt.jar,8893 
app-platform,intradeframework-app-platform.jar,8894 
app-trade,intradeframework-app-trade.jar,8895 
app-user,intradeframework-app-user.jar,8896 
app-wsquote,intradeframework-app-wsquote.jar,8897 
app-wsorder,intradeframework-app-wsorder.jar,8899 
)
######## JAR 包定义 #######

# JAR 包路径
WORK_PATH=/root/intrade-app
if [ ! -d $WORK_PATH  ];then
  mkdir $WORK_PATH
fi

# 日志路径
LOG_PATH=$WORK_PATH/logs
if [ ! -d $LOG_PATH  ];then
  mkdir $LOG_PATH
fi


main(){
clear
while true
do
echo "============================================================================="
echo "=                            JAR-程序维护 V1.0                              ="
echo "=                            provide by bobby                               ="
echo "=                                                                           ="
echo "=                            1.检查服务状态                                 ="
echo "=                            2.启动所有服务                                 ="
echo "=                            3.停止所有服务                                 ="
echo "=                            4.重启所有服务                                 ="
echo "=                            5.启动单个服务                                 ="
echo "=                            6.停止单个服务                                 ="
echo "=                            7.重启单个服务                                 ="
echo "=                            8.查看服务日志                                 ="
echo "=                            9.退出                                         ="
echo "============================================================================="

read -p "请选择一个操作：" num 
case $num in
1) check_status;;
2) start_all;;
3) stop_all;;
4) restart_all;;
5) service_start;;
6) service_stop;;
7) service_restart;;
8) service_log;;
9) exit;;
*) echo "指令错误，请重新选择!"
sleep 1
clear;;
esac
done
}

check_status(){
clear
echo "============================================================================="
echo "=                                                                           ="
echo "=                            检查服务状态                                   ="
echo "=                                                                           ="
echo "============================================================================="
for el in ${jar_arr[*]}
do
# 字符串分割为数组
el_arr=(${el//,/ })
alias=${el_arr[0]}
jar=${el_arr[1]}

P_ID=`ps -ef | grep -w $jar | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo -e "$alias process \033[31m stoped \033[0m"
        else
            echo -e "$alias process \033[32m running \033[0m"
        fi
		
done
}

start_all(){
clear
echo "============================================================================="
echo "=                                                                           ="
echo "=                          启动所有服务                                     ="
echo "=                                                                           ="
echo "============================================================================="
for el in ${jar_arr[*]}
do
# 字符串分割为数组
el_arr=(${el//,/ })
alias=${el_arr[0]}
jar=${el_arr[1]}
port=${el_arr[2]}

pid=`lsof -i:$port | grep "LISTEN"|awk '{print $2}'`
if [ "$pid" != "" ]; then
continue
fi
echo "****  开始启动  $alias   ...."
nohup java -jar $WORK_PATH/$jar >> $LOG_PATH/$jar.log 2>&1 &
until [ -n "$pid" ]
	do
	  pid=`lsof -i:$port | grep "LISTEN"|awk '{print $2}'` 
	  sleep 5
	done
echo -e "\033[32m 成功启动 $alias \033[0m"			
done
echo "all is running!!!" 
}

stop_all(){
clear
echo "============================================================================="
echo "=                                                                           ="
echo "=                          停止所有服务                                     ="
echo "=                                                                           ="
echo "============================================================================="
for el in ${jar_arr[*]}
do
# 字符串分割为数组
el_arr=(${el//,/ })
alias=${el_arr[0]}
jar=${el_arr[1]}

pid=`ps -ef | grep -w $jar | grep -v "grep" | awk '{print $2}'`
if [ "$pid" != "" ]; then
	kill -9 $pid
	echo $alias' is killed'
fi
done
echo "stop all success!!!"		
}

restart_all(){
stop_all
sleep 3
start_all
}

service_start(){
clear
while true
do
echo "============================================================================="
i=0
for el in ${jar_arr[*]}
do
el_arr=(${el//,/ })
alias=${el_arr[0]}
let i++
echo "=                        $i.启动 $alias                                 ="
done
echo "=                        $((i+1)).返回上级菜单                                    ="
echo "=                        $((i+2)).退出                                            ="
echo "============================================================================="
read -p "请选择一个操作：" num
len=${#jar_arr[@]}
if [ $num -lt 1  -o $num -gt $((len+2))]; then
	echo "指令错误，请重新选择!"
	# sleep 1
	# clear
elif [ $num -eq $((len+1)) ]; then
	main
elif [ $num -eq $((len+2)) ]; then
	exit
else
	el=${jar_arr[$((num-1))]}
	el_arr=(${el//,/ })
	alias=${el_arr[0]}
	jar=${el_arr[1]}
	port=${el_arr[2]}

	pid=`lsof -i:$port | grep "LISTEN"|awk '{print $2}'`
	if [ "$pid" != "" ]; then
		echo "$alias is running!!!"
	else
		echo "****  开始启动  $alias   ...."
		nohup java -jar $WORK_PATH/$jar >> $LOG_PATH/$jar.log 2>&1 &
		until [ -n "$pid" ]
			do
			pid=`lsof -i:$port | grep "LISTEN"|awk '{print $2}'` 
			sleep 5
			done
		echo -e "\033[32m 成功启动 $alias  \033[0m"
	fi
fi
done
}

service_stop(){
clear
while true
do
echo "============================================================================="
i=0
for el in ${jar_arr[*]}
	do
	el_arr=(${el//,/ })
	alias=${el_arr[0]}
	let i++
	echo "=                        $i.停止 $alias                                 ="
	done
echo "=                        $((i+1)).返回上级菜单                                    ="
echo "=                        $((i+2)).退出                                            ="
echo "============================================================================="
len=${#jar_arr[@]}
read -p "请选择一个操作：" num
case $num in
[1-$len]) 
el=${jar_arr[$((num-1))]}
el_arr=(${el//,/ })
alias=${el_arr[0]}
jar=${el_arr[1]}

pid=`ps -ef | grep -w $jar | grep -v "grep" | awk '{print $2}'`
if [ "$pid" != "" ]; then
	kill -9 $pid
	echo $alias' is killed'
fi;;
$((len+1))) main;;
$((len+2))) exit;;
*) echo "指令错误，请重新选择!"
sleep 1
clear;;
esac
done
}

service_restart(){
clear
while true
do
echo "============================================================================="
i=0
for el in ${jar_arr[*]}
	do
	el_arr=(${el//,/ })
	alias=${el_arr[0]}
	let i++
	echo "=                        $i.重启 $alias                                ="
	done
echo "=                        $((i+1)).返回上级菜单                                    ="
echo "=                        $((i+2)).退出                                            ="
echo "============================================================================="
len=${#jar_arr[@]}
read -p "请选择一个操作：" num
case $num in
[1-$len]) 
el=${jar_arr[$((num-1))]}
el_arr=(${el//,/ })
alias=${el_arr[0]}
jar=${el_arr[1]}
port=${el_arr[2]}

# 先停止服务
pid=`ps -ef | grep -w $jar | grep -v "grep" | awk '{print $2}'`
if [ "$pid" != "" ]; then
	kill -9 $pid
	echo $alias' is killed'
fi
# 在启动服务
pid=`lsof -i:$port | grep "LISTEN"|awk '{print $2}'`
if [ "$pid" != "" ]; then
	echo "$alias is running!!!"
else
	echo "****  开始启动  $alias   ...."
	nohup java -jar $WORK_PATH/$jar >> $LOG_PATH/$jar.log 2>&1 &
	until [ -n "$pid" ]
	do
	  pid=`lsof -i:$port | grep "LISTEN"|awk '{print $2}'` 
	  sleep 5
	done
echo -e "\033[32m 成功启动 $alias  \033[0m"
fi
;;
$((len+1))) main;;
$((len+2))) exit;;
*) echo "指令错误，请重新选择!"
sleep 1
clear;;
esac
done
}

service_log(){
clear
while true
do
echo "============================================================================="
i=0
for el in ${jar_arr[*]}
	do
	el_arr=(${el//,/ })
	alias=${el_arr[0]}
	let i++
	echo "=                        $i.查看 $alias 日志                                ="
	done
echo "=                        $((i+1)).返回上级菜单                                    ="
echo "=                        $((i+2)).退出                                            ="
echo "============================================================================="
len=${#jar_arr[@]}
read -p "请选择一个操作：" num
case $num in
[1-$len]) 
el=${jar_arr[$((num-1))]}
el_arr=(${el//,/ })
jar=${el_arr[1]}

tail -f $LOG_PATH/$jar.log
;;
$((len+1))) main;;
$((len+2))) exit;;
*) echo "指令错误，请重新选择!"
sleep 1
clear;;
esac
done
}

main
